<?php

namespace App\Kuafu\Config\Controller\B;

use App\Kuafu\Config\Enum\CodeEnum;
use App\Kuafu\Config\Enum\ConfigCodeEnum;
use App\Kuafu\Config\Exceptions\ConfigException;
use App\Kuafu\Config\Model\ConfigModel;
use App\Kuafu\Config\Service\ConfigService;
use App\Kuafu\Config\Validate\ConfigValidate;
use Illuminate\Http\Request;
use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Base\Service\BaseService;

class ConfigController extends BaseController
{
    /**
     * @param $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addConfig(Request $request)
    {
        $name   = (string)$request->post('name');
        $key    = (string)$request->post('key');
        $value  = (string)$request->post('value');
        $type   = (string)$request->post('type');
        $remark = (string)$request->post('remark');

        $valid = ConfigValidate::addConfig($request);
        if ($valid) {
            return $this->fail(ConfigCodeEnum::ERR_CONFIG_PARAM, $valid);
        }

        ConfigService::addConfig($name, $key, $value, $type, $remark);
        return $this->success();
    }

    /**
     * 获取配置列表
     * @param $request
     */
    public function getConfigList(Request $request)
    {
        return $this->success(ConfigService::getConfigList($request));
    }

    /**
     * 根据id获取配置详情
     * @param $request
     * @param $configId
     */
    public function getConfigById(Request $request, $configId)
    {
        return $this->success(ConfigService::getConfigById($request, $configId));
    }

    /**
     * 删除配置（软删除）
     * @param $request
     * @param $configId
     */
    public function delConfig(Request $request, $configId)
    {
        ConfigService::delConfig($request, $configId);
        return $this->success();
    }

    /**
     * 根据id修改配置信息
     * @param $request
     */
    public function setConfig(Request $request)
    {
        ConfigService::setConfig($request);
        return $this->success();
    }

    /**
     * 根据type查询列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function listByTypeAndKey(Request $request)
    {
        $type = (string)$request->get('type');
        $key  = (string)$request->get('key');

        $lists = ConfigService::listByTypeAndKey($type, $key);
        $data  = [
            'lists' => $lists,
        ];

        return $this->success($data);
    }

    //修改商家订货通知配置
    public function setAppNotify(Request $request)
    {
        $data['notify']                      = $request->get('notify', CodeEnum::NOTIFY_ENABLE);                   //是否通知
        $data['advanceNoticeTime']           = $request->get('advanceNoticeTime', 8);        //提前通知的时间
        $data['advanceNoticeMethod']         = $request->get('advanceNoticeMethod', CodeEnum::NOTIFY_TYPE[CodeEnum::NOTIFY_TYPE_APP]);      //提前通知的方式
        $data['notificationTimeOfDay']       = $request->get('notificationTimeOfDay', 8);    //当天通知的时间
        $data['SameDayficationMethod']       = $request->get('SameDayfication', CodeEnum::NOTIFY_TYPE[CodeEnum::NOTIFY_TYPE_SMS]);          //当天通知的方式
        $data['noticeAFewHoursAgo']          = $request->get('noticeAFewHoursAgo', 3);       //订货结束前几小时通知
        $data['noticeAFewHoursAgoMethod']    = $request->get('noticeAFewHoursAgoMethod', CodeEnum::NOTIFY_TYPE[CodeEnum::NOTIFY_TYPE_SMS]); //订货结束前几小时通知方式
        return $this->success(ConfigService::setAppNotify($data));
    }

    public function allModules(Request $request)
    {
        return $this->success(ConfigService::allModules());
    }

    public function index(Request $request)
    {
        $module   = (string)$request->get('module');
        $name     = (string)$request->get('name');
        $pageSize = (int)$request->get('pageSize', 20);
        $pageNum  = (int)$request->get('pageNum', 1);

        return $this->success(ConfigService::index($module, $name, $pageSize, $pageNum));
    }

    public function save(Request $request)
    {
        $this->validate($request, [
            'id'          => 'required|integer',
            'description' => 'required|string',
            'value'       => 'required',
            'module'      => 'required',
            'name'        => 'required',
        ]);

        $id          = $request->get('id');
        $description = $request->get('description');
        $value       = $request->get('value');
        $module      = $request->get('module');
        $name        = $request->get('name');

        ConfigService::save($id, $description, $value, $module, $name);

        return $this->success();
    }
}
